﻿using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

/**
 * 暂时没法用更好的方式，把添加模型的代码放到这里
 */
public class ModelController : MonoBehaviour
{
    //private GameObject modelPrefab;          //模型    //不再拖动添加，而是通过代码加载
    private GameObject mModelPrefabResObj;
    // public GameObject bloodPrefab;        //血池特效
    // public GameObject tornadoPrefab;      //旋风特效

    private AudioSource mAudioSource;        //音效源

    private Button mBtnAddModel;
    private bool mIsTrackingFound = false;

    void Awake()
    {
        mModelPrefabResObj = (GameObject)Resources.Load("Prefabs/Astronaut");
        //为人物模型添加手势脚本
        mModelPrefabResObj.AddComponent<Player>();
        mModelPrefabResObj.AddComponent<PlayerRotate>();
        mModelPrefabResObj.AddComponent<EnLarge>();

        //这里初始化 mModelPrefabResObj 则 addModel() 在 GameObject.Instantiate(modelPrefab, xxx) 总是提示:
        //MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
        //
        //modelPrefab = (GameObject)PrefabUtility.InstantiatePrefab(mModelPrefabResObj); //如果使用该方法则 Build apk 时报错: The name 'PrefabUtility' does not exist in the current context
        //modelPrefab = Instantiate(mModelPrefabResObj); //使用该方法会造成 prefab 的引用关联丢失?
        //LogUtils.debug("ModelController Awake() modelPrefab =" + modelPrefab);

        mBtnAddModel = (Button)GameObject.Find("Canvas/ModelControlButton").GetComponent<Button>();
        LogUtils.debug("ModelController Start() 找到 mBtnAddModel = " + mBtnAddModel);
        mAudioSource = this.GetComponent<AudioSource>();
    }

    protected virtual void Start()
    {
        mBtnAddModel.onClick.AddListener(OnAddModelBtnClick);
        mBtnAddModel.gameObject.SetActive(false);
    }

    public void OnTrackingFoundInMC()
    {
        LogUtils.debug("ModelController OnTrackingFoundInMC()");
        ToastUtil.showToast("ModelController OnTrackingFoundInMC()");
        mIsTrackingFound = true;
        mBtnAddModel.gameObject.SetActive(true);
        //addModel();                //不是检测到就添加模型，而是点击按钮再添加模型
    }

    public void OnTrackingLostInMC()
    {
        LogUtils.debug("ModelController OnTrackingLost()");
        mIsTrackingFound = false;
        mBtnAddModel.gameObject.SetActive(false);
        destroyModel();
    }

    public void OnAddModelBtnClick()
    {
        LogUtils.debug("ModelController OnAddModelBtnClick mIsTrackingFound=" + mIsTrackingFound);
        if (mIsTrackingFound)
        {
            ToastUtil.showToast("点击了按钮，并且找到了图片，开始添加模型");
            addModel();
        }
        else
        {
            ToastUtil.showToast("点击了按钮，但是没有找到图片，无法添加模型");
        }
    }

    private void addModel()
    {
        //如果声音没有播放，那么才播放
        if (!mAudioSource.isPlaying)
        {
            mAudioSource.Play();
        }

        //GameObject modelObj = GameObject.Instantiate(modelPrefab);
        //modelObj.transform.position = this.transform.position;
        //modelObj.transform.parent = this.transform;

        //position 向下平移 1.6 //旋转随着待识别的图片(扑克牌)而旋转，所以用 this.transform.rotation
        LogUtils.debug("ModelController addModel() mModelPrefabResObj = " + mModelPrefabResObj);
        Quaternion quaternion = Quaternion.Inverse(this.transform.rotation); //不转一下总是倒立着
        GameObject modelObj = GameObject.Instantiate(mModelPrefabResObj, this.transform.position - new Vector3(0, 0, 0), quaternion);

        //可以 Instantiate GameObject 时直接指定位置，位置 position 使用当前识别到的卡片的 position，旋转 Quaternion 用它自己的
        // GameObject effect1 = GameObject.Instantiate(bloodPrefab, this.transform.position, Quaternion.identity);
        // effect1.transform.parent = this.transform;
        // //延迟 7s 销毁 GameObject
        // Destroy(effect1, 6.0f);

        // GameObject effect2 = GameObject.Instantiate(tornadoPrefab, this.transform.position, Quaternion.identity);
        // effect2.transform.parent = this.transform;
        // Destroy(effect2, 6.0f);
    }

    private void destroyModel()
    {
        LogUtils.debug("ModelController destroyModel()");

        //不 Destroy 的话每创建一个模型都会一直在
        Destroy(GameObject.Find("Astronaut(Clone)"));               //名字就是 Assets/Prefabs/对应 Prefab 的名字
        // Destroy(GameObject.Find("RFX_Blood_Puddle(Clone)"));
        // Destroy(GameObject.Find("RFX_Tonado_Flame(Clone)"));
    }
}